<!-- loginPage -->
<template>
  <div class='login'>
    <div
      class="verify"
      v-show="!msg"
    >
      <slide-verify
      ref="slideblock"
       v-loading="loading2"
        :l="42"
        :r="10"
        :w="310"
        :h="155"
        slider-text="向右滑动"
        @success="onSuccess"
        @fail="onFail"
        @refresh="onRefresh"
         @fulfilled="noFulfilled"
      ></slide-verify>
    </div>
    <div
      class="box"
      v-show="msg"
    >
      <el-card class="login-form-layout">
        <el-form
          autoComplete="on"
          :model="loginForm"
          :rules="loginRules"
          ref="loginForm"
          label-position="left"
        >
          <div style="text-align: center">
            <i
              class="el-icon-eleme"
              style="width: 56px;height: 56px;color: #67C23A;font-size:50px"
            ></i>
          </div>
          <h2
            class="login-title color-main"
            style="color:#67C23A"
          >注册</h2>
          <el-form-item prop="username">
            <el-input
              name="username"
              type="text"
              v-model="loginForm.username"
              autoComplete="on"
              placeholder="请输入用户名"
            >
              <span slot="prefix">
                <i class="el-icon-user-solid"></i>
              </span>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              name="password"
              :type="pwdType"
              @keyup.enter.native="handleRegister"
              v-model="loginForm.password"
              autoComplete="on"
              placeholder="请输入密码"
            >
              <span slot="prefix">
                <i class="el-icon-lock"></i>
              </span>
              <span
                slot="suffix"
                @click="showPwd"
              >
                <i class="el-icon-view"></i>
              </span>
            </el-input>
          </el-form-item>
          <el-form-item style="margin-bottom: 60px;text-align: center">
            <el-button
              style="width: 45%"
              type="primary"
              :loading="loading"
              @click.native.prevent="handleRegister "
            >
              注册
            </el-button>
            <el-button
              style="width: 45%"
              type="primary"
              @click.native.prevent="linkToLogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
import { isvalidUsername } from '@/utils/validate';
import { UPDATE_USER_INFO } from "../../vuex/mutationsType"
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('密码不能小于3位'))
      } else {
        callback()
      }
    };
    return {
      loginForm: {
        username: '',
        password: '',
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      loading: false,
      pwdType: 'password',
      msg: '未验证',
       loading2:false
    }
  },
  methods: {
    onSuccess() {
      this.msg = 'login success';
      var username = this.loginForm.username;
      var password = this.loginForm.password;
      var time = new Date();
      var email = Math.floor(Math.random() * 0x1000000).toString(16).padStart(6, "0") + '@qq.com'
      this.loading = true;
      /* 注册接口 */
      this.$axios
        .post("http://49.233.65.169:3000/api/register", {
          account: username,
          username,
          password,
          lasttiem: time,
          starttiem: time,
          logintiem: time,
          email
        })
        .then(res => {
          if (res.code === 0) {
            this.loading = false;
            this.$message({
              showClose: true,
              message: '注册成功！请登录',
              type: 'success'
            });
            this.$router.push('/login'); // 跳转回首页
          } else {
            this.loading = false;
            this.$message({
              showClose: true,
              message: '用户名已存在',
              type: 'error'
            });
          }
          this.$refs.slideblock.reset();
        })
        .catch(err => err);
    },
    onFail() {
       this.$message({
              showClose: true,
              message: '验证失败',
              type: 'error'
            });
      this.msg = ''
       this.loading2=true
       
    },
    onRefresh() {
      this.msg = ''
    },
    noFulfilled(){
      this.loading2=false
    },
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    linkToLogin() {
      this.$message({
        showClose: true,
        message: '跳转到登录界面',
      });
      this.$router.push('/login');
    },
    handleRegister() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.msg = "";
        } else {
          console.log('参数验证不合法！');
          return false
        }
      })
    },
    linkToSign() {
      this.$router.push('/register')
    }

  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合

  //生命周期 - 创建完成（可以访问当前this实例）
  created() {

  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() { }, //生命周期 - 创建之前
  beforeMount() { }, //生命周期 - 挂载之前
  beforeUpdate() { }, //生命周期 - 更新之前
  updated() { }, //生命周期 - 更新之后
  beforeDestroy() { }, //生命周期 - 销毁之前
  destroyed() { }, //生命周期 - 销毁完成
  activated() { }, //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style  scoped>
.login {
  background: url("https://www.battlenet.com.cn/login/static/images/login/background/login-background-1280.4JLnG.jpg");
  height: 100vh;
}
.login-form-layout {
  position: absolute;
  left: 0;
  right: 0;
  width: 360px;
  margin: 140px auto 0;
  border-top: 10px solid #67c23a;
}

.login-title {
  text-align: center;
}

.login-center-layout {
  background: #67c23a;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  margin-top: 200px;
}
i {
  color: #67c23a;
}
.el-button--primary {
  background-color: #67c23a;
  border-color: #67c23a;
}
.verify {
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -155px;
  margin-top: -104px;
  z-index: 999;
   background-color:#ffffff;
}
</style>